<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hotel_index_module extends CI_Module
{
    private $data;
    private $sid;

    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->data = $GLOBALS['config'];
        $this->load->model('hotel.Hotel_model');
        $this->load->model('order.Orderinfo_model');
        $this->load->model('order.Ordercontacts_model');
        $this->load->model('order.OrderHotel_model');
        $this->load->model('admin.Area_model');
        $this->load->model('member.User_model');

    }

    /*
     * 酒店频道首页
     * */

    public function index()
    {


        $this->data['sitecityname'] = $this->data['sitecityname'];
        $this->data['form_url'] = site_url('hotel/index/search');
        $this->data['area_list'] = $this->Area_model->get_area_by_id($this->data['sitecity']);
        $this->data['params'] = array(
            'seo'=>array(
                'title'=>SITECITYNAME.''.$title_query.'酒店_'.SITECITYNAME.''.$title_query.'汽车租赁_'.SITECITYNAME.'旅行社_'.$this->data['sitename'],
                'keywords'=>SITECITYNAME.''.$title_query.'酒店,'.SITECITYNAME.''.$title_query.'自驾酒店',
                'desc'=>SITECITYNAME.''.$title_query.'酒店,'.$this->data['sitename'].'为您提供'.SITECITYNAME.''.$title_query.'汽车租赁信息，在这里有大量的'.SITECITYNAME.'酒店信息供您选择。'.SITECITYNAME.'旅游网'
            ),
            'nav'=>array(
                'current'=>'rent'
            )
        );

        $this->data['bread'] = array(
            array('name'=>SITECITYNAME.'酒店','url'=>site_url('zuche')),
        );
        $this->load->view('default/index', $this->data);
    }


    /*
     * 酒店搜索列表
     */

    public function search()
    {
        $title_query = '';
        $title_query = '';
        $where= "sid = ".$this->sid." AND status = 1" ;
        $param = $this->uri->segment(2);
        $param = str_replace(array('list_'), '', $param);
        @list($service_type, $seat, $car_type) = explode('_', $param);
        $service_type = $this->data['service_type'] = intval($service_type);
        $seat     = $this->data['seat']     = intval($seat);
        $car_type    = $this->data['car_type']    = intval($car_type);
        if($service_type)
        {
            switch($service_type)
            {
                case "1" :
                    $where .= " AND airport_service = 1";

                    break;

                case "2" :
                    $where .= " AND short_service = 1";
                    break;

                case "3" :
                    $where .= " AND long_service = 1";
                    break;
            }

            $title_query = $this->Hotel_model->return_service_type($service_type);
        }

        if($seat)
        {
            $where .= " AND seat = ".$seat;
            $title_query .= $this->Hotel_model->return_seat_number($seat);
        }

        if($car_type)
        {
            $where .= " AND car_type = ".$car_type;
            $title_query .= $this->Hotel_model->return_car_type($car_type);
        }
        $page_config['perpage']=10;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']= "/zuche/list_{$service_type}_{$seat}_{$car_type}";//url
        $page_config['nowindex'] = $this->uri->segment(3)>0 ? $this->uri->segment(3):1;//当前页
        $page_config['total'] = $this->Hotel_model->get_hotel_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $hotel_list = $this->Hotel_model->get_hotel_list($where,$page_config['perpage'],$page_config['nowindex']);
        if($hotel_list)
        {
           foreach($hotel_list as $key=>$val)
           {
               $hotel_list[$key]['hotel_type'] = $this->Hotel_model->return_hotel_type($val['star']);
               $hotel_list[$key]['price_list'] = $this->Hotel_model->get_price_list(array('hotel_id'=>$val['id'],'go_date'=>date('Y-m-d',time())));
               $hotel_list[$key]['min_price'] = $this->Hotel_model->_get_min_price($hotel_list[$key]['price_list']);
               $hotel_list[$key]['detail_url'] = site_url('jiudian/'.$val['id'].'.html');
            }
        }
        $this->data['list'] = $hotel_list ;
        $this->data['total_num'] =  $page_config['total'];
        $this->data['params'] = array(
            'seo'=>array(
                'title'=>SITECITYNAME.''.$title_query.'酒店_'.SITECITYNAME.''.$title_query.'汽车租赁_'.SITECITYNAME.'旅行社_'.$this->data['sitename'],
                'keywords'=>SITECITYNAME.''.$title_query.'酒店,'.SITECITYNAME.''.$title_query.'自驾酒店',
                'desc'=>SITECITYNAME.''.$title_query.'酒店,'.$this->data['sitename'].'为您提供'.SITECITYNAME.''.$title_query.'汽车租赁信息，在这里有大量的'.SITECITYNAME.'酒店信息供您选择。'.SITECITYNAME.'旅游网'
            ),
            'nav'=>array(
                'current'=>'rent'
            )
        );
        $this->data['bread'] = array(
            array('name'=>SITECITYNAME.'酒店','url'=>site_url('zuche')),
        );
        $this->data['area_list'] = $this->Area_model->get_area_by_id($this->data['sitecity']);
        print_r($this->data['list']);
        $this->load->view('default/search', $this->data);
    }


    public function detail($id)
    {
        $this->data['info'] = $this->Hotel_model->_hotel_info($id);
        print_r($this->data['info']);
        $this->data['price_list'] = $this->Hotel_model->get_price_list(array('hotel_id'=>$id,'go_date'=>date('Y-m-d',time())));
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $param = $this->input->get_post('param');
            $this->data['price_list'] = $this->Hotel_model->get_price_list(array('hotel_id'=>$id,'go_date'=>$param['go_date']));
        }
        $title_query = $this->data['info']['name'];
        $this->data['form_url'] =
        $this->data['params'] = array(
            'seo'=>array(
                'title'=>SITECITYNAME.''.$title_query.'酒店_'.SITECITYNAME.''.$title_query.'汽车租赁_'.SITECITYNAME.'旅行社_'.$this->data['sitename'],
                'keywords'=>SITECITYNAME.''.$title_query.'酒店,'.SITECITYNAME.''.$title_query.'自驾酒店',
                'desc'=>SITECITYNAME.''.$title_query.'酒店,'.$this->data['sitename'].'为您提供'.SITECITYNAME.''.$title_query.'汽车租赁信息，在这里有大量的'.SITECITYNAME.'酒店信息供您选择。'.SITECITYNAME.'旅游网'
            ),
            'nav'=>array(
                'current'=>'rent'
            )
        );
        $this->data['bread'] = array(
            array('name'=>SITECITYNAME.'酒店','url'=>site_url('zuche')),
        );
        $this->load->view('default/detail', $this->data);
    }


    public function head()
    {
        $this->load->view('default/page_header');
    }

    public function foot()
    {
        $this->load->view('default/page_footer');
    }

    public function order()
    {
        $id = $this->input->get_post('id');
        @$user_id = $this->session->userdata('user_id');
        if($user_id<1){
            redirect('denglu?referer='.urlencode(site_url('hotel/index/order?id='.$id)));
        }
        $this->data['price_info'] = $this->Hotel_model->_hotel_price_info($id);
        $this->data['price_info']['room_type_name'] = $this->Hotel_model->return_room_type($this->data['price_info']['room_type']);
        $this->data['hotel_info'] = $this->Hotel_model->_hotel_info($this->data['price_info']['hotel_id']);
        $this->data['hotel_info']['star_name'] = $this->Hotel_model->return_hotel_type($this->data['hotel_info']['star']);
        $this->data['go_id'] = $id ;
        $this->data['form_url'] = site_url('hotel/index/order') ;
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            @$go_id = intval($_POST['id']);
            @$contact_name = trim($_POST['contact_name']);
            @$contact_phone = trim($_POST['contact_phone']);
            if(
            empty($go_id) ||  empty($contact_name) || empty($contact_phone)
            ){
                $this->error('数据异常，订单生成失败！', '', TRUE);
            }


            //订单信息
            $order_data = array(
                'sid' =>$this->sid,
                'order_sn'      => $this->Orderinfo_model->set_order_sn(),
                'order_type'    => 5,//门票订单
                'cat_type'      => 2,//网上订单
                'order_account' => $this->data['price_info']['ws_price'],
                'order_status'  => 0 , //酒店订单需要审核
                'order_note'  => @trim($_POST['order_note']), //门票无需审核
                'pay_status'    => 1,//未支付
                'add_time'      => date('Y-m-d H:i:s',time()),
                'user_id'       => $user_id,//用户ID
                'admin_user_id' => $this->session->userdata('admin_user_id'),//管理员ID
            );
            $order_id = $order_return_status = $this->Orderinfo_model->add_row($order_data);

            if($order_id){
                //联系人信息
                $contact_data = array(
                    'order_id'  => $order_id,
                    'name'      => $contact_name,
                    'phone_num' => $contact_phone,
                );
                $contact_return_status = $this->Ordercontacts_model->add_row($contact_data);

                //订单酒店信息
                $row = array(
                    'sid' =>$this->sid,
                    'order_id'  => $order_id,
                    'go_id' => $go_id,
                    'hotel_id' =>  $this->data['price_info']['hotel_id'],
                 );
                $add_return_status = $this->OrderHotel_model->add_row($row);

                if($contact_return_status&&$add_return_status)
                {
                    $submit_template = $GLOBALS['config']['hotel_order_template_submit'];
                    $target = array(
                        '{$order_sn}',
                        '{$pay_money}',
                        '{$date}',
                        '{$brand}',
                    );
                    $replace = array(
                        $order_data['order_sn'],
                        $order_data['order_account'],
                        $row['go_date'],
                        $this->data['car_info']['brand']
                    );

                    $content = str_replace($target,$replace,$submit_template);
                    $this->User_model->_send_sms($contact_data['phone_num'],$content);

                    $this->success('提交订单成功',site_url('hotel/index/succeed'),true);
                }
                else
                {
                    $this->error('订单生成失败!', '',true);
                }
            }
            else
            {
                $this->error('订单生成失败!', '',true);
            }
        }

        $this->load->view('default/order', $this->data);
    }

    /**
     * 酒店订单成功
     */
    public function succeed()
    {
        $this->load->view('default/succeed', $this->data);
    }


}